package com.company.cloud.user.client;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.captcha.modules.util.StringUtils;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.user.modules.api.request.ValidateUserRequest;
import com.company.cloud.user.modules.api.response.ValidateUserResponse;
import com.company.cloud.user.modules.base.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.user.modules.api.request.RegisterEmployeeRequest;
import com.company.cloud.user.modules.api.response.UserFull;
import com.company.cloud.user.modules.api.service.UserRegisterRemoteService;
import com.company.cloud.user.modules.api.request.UserRegisterParam;
import com.company.cloud.user.modules.app.service.UserRegisterService;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;

@RestController
@Service
public class UserRegisterRemoteServiceImpl implements UserRegisterRemoteService {
    @Autowired
    private UserRegisterService userRegisterService;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public BaseClientResult<UserFull> register(RegisterEmployeeRequest params) {
        try {
            UserRegisterParam pa = UserConvertUtils.getUserRegisterParam(params);
            UserInfo user= userRegisterService.register(pa, null);
            return BaseClientResult.success(UserConvertUtils.getUserFull(user));
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<ValidateUserResponse> validateUserName(ValidateUserRequest params) {
        try {
            if(params==null || StringUtils.isEmpty(params.getUserName())){
                return BaseClientResult.success();
            }
            int num= userInfoService.count(new QueryWrapper<UserInfo>().eq("user_name",params.getUserName()));
            ValidateUserResponse data=new ValidateUserResponse();
            data.setFlag(num>0?BaseConstant.YNEnum.YES.getCode():BaseConstant.YNEnum.NO.getCode());
            return BaseClientResult.success(data);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }



}
